﻿#region "Copyright (C) Lenny Granovsky. 2005-2013"
//This program is free software distribute under the terms of the GNU General Public License as published by
//the Free Software Foundation. Please read AssemblyInfo.cs file for more information.
#endregion

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Web.Enhancements.Rest.Filters
{
    public class ValidateRequestFilter : IWebRestFilter
    {
        public bool RequiresValidation { get; set; }

        public ValidateRequestFilter()
        {
        }


        /// <summary>
        /// Method that is called prior the REST service handler created. This allows to cancel the call at earlier stages. 
        /// </summary>
        /// <remarks>The session and list parameters are not available at this time.</remarks>
        /// <param name="httpContext">Current HTTP content.</param>
        /// <param name="serviceContext">Current REST service context.</param>
        public void BeforeHandlerCreated(System.Web.HttpContext httpContext, IWebRestServiceContext serviceContext)
        {
        }

        public void BeforeMethodCall(System.Web.HttpContext httpContext, IWebRestServiceContext serviceContext)
        {
            //verify if method requires request validation - validate if should
            if (this.RequiresValidation)
                httpContext.Request.ValidateInput();//method will throw an exception if request has bad data
        }

        public void AfterMethodCall(System.Web.HttpContext context, ref object returnValue)
        {
        }
    }
}
